package org.intellij.lang.batch.runner;

import com.intellij.execution.configuration.EnvironmentVariablesComponent;
import com.intellij.ide.util.BrowseFilesListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.ui.RawCommandLineEditor;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * @author wibotwi
 */
public class BatchCommonOptionsForm implements CommonBatchRunConfigurationParams {
    private JPanel rootPanel;
    private EnvironmentVariablesComponent envsField;
    private TextFieldWithBrowseButton workingDirectoryField;
    private RawCommandLineEditor interpreterOptionsField;

    public BatchCommonOptionsForm(BatchRunConfiguration runConfiguration) {
        workingDirectoryField.addBrowseFolderListener("Select Working Directory", "", runConfiguration.getProject(), BrowseFilesListener.SINGLE_DIRECTORY_DESCRIPTOR);
    }

    public JPanel getRootPanel() {
        return rootPanel;
    }

    public String getInterpreterOptions() {
        return interpreterOptionsField.getText().trim();
    }

    public void setInterpreterOptions(String options) {
        interpreterOptionsField.setText(options);
    }

    public String getWorkingDirectory() {
        return FileUtil.toSystemIndependentName(workingDirectoryField.getText().trim());
    }

    public void setWorkingDirectory(String workingDirectory) {
        workingDirectoryField.setText(FileUtil.toSystemDependentName(workingDirectory));
    }

    public boolean isPassParentEnvs() {
        return envsField.isPassParentEnvs();
    }

    public void setPassParentEnvs(boolean passParentEnvs) {
        envsField.setPassParentEnvs(passParentEnvs);
    }

    public Map<String, String> getEnvs() {
        return envsField.getEnvs();
    }

    public void setEnvs(Map<String, String> envs) {
        envsField.setEnvs(envs);
    }

    public TextFieldWithBrowseButton getWorkingDirectoryField() {
        return workingDirectoryField;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        rootPanel = new JPanel();
        rootPanel.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 0, 0), -1, -1));
        final JLabel label1 = new JLabel();
        this.$$$loadLabelText$$$(label1, ResourceBundle.getBundle("org/intellij/lang/batch/util/BatchBundle").getString("runcfg.labels.working_directory"));
        rootPanel.add(label1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        workingDirectoryField = new TextFieldWithBrowseButton();
        rootPanel.add(workingDirectoryField, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, new Dimension(150, -1), null, null, 0, false));
        envsField = new EnvironmentVariablesComponent();
        envsField.setText(ResourceBundle.getBundle("org/intellij/lang/batch/util/BatchBundle").getString("runcfg.labels.environment_variables"));
        rootPanel.add(envsField, new GridConstraints(0, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label2 = new JLabel();
        this.$$$loadLabelText$$$(label2, ResourceBundle.getBundle("org/intellij/lang/batch/util/BatchBundle").getString("runcfg.labels.interpreter_options"));
        rootPanel.add(label2, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        interpreterOptionsField = new RawCommandLineEditor();
        interpreterOptionsField.setDialogCaption(ResourceBundle.getBundle("org/intellij/lang/batch/util/BatchBundle").getString("runcfg.captions.interpreter_options_dialog"));
        rootPanel.add(interpreterOptionsField, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, new Dimension(150, -1), null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return rootPanel;
    }
}
